<?php

/**
 * Controller_change
 */

class Controller_change extends MyApp {

	public function __construct()
	{
		parent::__construct();
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->record = YOU::loadModel('record');
		$this->model->user   = YOU::loadModel('user');
		$this->model->staff   = YOU::loadModel('staff');
	}

	/** 首页 **/
	public function actionIndex()
	{

	}
	/** 人事变动记录列表() **/
	public function actionList ()
	{
		// 进入教师，外聘的入口
		$personnel_type = $this->get->personnel_type;
		$staff_id = $this->get->staff_id;
		// 通过id获取老师名称
		if (!empty($staff_id)) {
			$staff_data = $this->model->staff->getInfo($staff_id);
			$staff_name = $staff_data['staff_name'];
		}else{

			$staff_name = $this->get->staff_name;


		}
		// 查询条件
		$where = " ";

		$from = $this->get->from;
		$to = $this->get->to;
		if(!empty($staff_name)){

			$where .= " AND staff_name LIKE '%{$staff_name}%' ";
			$this->tpl->assign("staff_name", $staff_name);
		}
		if(!empty($from)){
			$where .= " AND start_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			$where .= " AND start_time <= '{$to}' ";
			$this->tpl->assign("to", $to);
		}


		if ($where != " ") {
			// 条数
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_personnel` WHERE personnel_type ='{$personnel_type}' {$where}");
			$count = $tmp['count'];

			$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_personnel` WHERE personnel_type ='{$personnel_type}' {$where} ORDER BY personnel_id DESC ");
		}else{
			$count = 0;
		}
		
		foreach($staff_lists as $key=>$value){
			// 在职标记
			$staff_lists[$key]['staff_sign'] = $this->sign_ini[$value['staff_sign']];
		}

		$this->tpl->assign("personnel_type", $personnel_type);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("count", $count);
		$this->tpl->assign("staff_id", $staff_id);

		$this->tpl->assign("staff_data", $staff_data);

		$this->tpl->display("change/m.changenotes.list.tpl.php");
	}
	/** 人事变动添加 **/
	public function actionAdd()
	{
		// 进入教室，外聘入口
		$personnel_type = (int)$this->get->personnel_type;

		$staff_id   	= (int)$this->get->staff_id;
		$staff_data 	= $this->model->staff->getinfo($staff_id);

		if (empty($staff_data)){
			Helper_Client::alert("人员不存在");
		}

		// 在职标记
		$staff_sign = $this->sign_ini;
		//时间日期转换
		$teacher_data['update_time'] = date("Y-m-d", strtotime($teacher_data['update_time']));
		if ($_POST){

			$staff_number    		= $this->post->staff_number;
			$staff_name      		= $this->post->staff_name;
			$personnel_variation 	= $this->post->personnel_variation;
			$start_time     		= $this->post->start_time;
			$personnel_from      	= $this->post->personnel_from;
			$personnel_to        	= $this->post->personnel_to;
			$staff_sign      		= $this->post->staff_sign;
			$personnel_remark    	= $this->post->personnel_remark;

			// 人事变动历史记录
			$info = array(
			"staff_id"     			=> $staff_id,
			"staff_number"     		=> $staff_data['staff_number'],
			"staff_name"       		=> $staff_name,
			"personnel_variation"  	=> $personnel_variation,
			"start_time"      		=> $start_time,
			"personnel_from"    	=> $personnel_from,
			"personnel_to"      	=> $personnel_to,
			"staff_sign"       		=> $staff_sign,
			"personnel_remark"  	=> $personnel_remark,
			"personnel_type"  		=> $personnel_type,
			"status"            	=> 1,
			"create_time"       	=> date("Y-m-d H:i:s"),
			"update_time"       	=> "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_personnel", $info);
			$info2 = array(
			"staff_variation"  	=> $personnel_variation,
			"staff_sign"       		=> $staff_sign,
			"update_time"       	=> "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_staff", $info2 ," staff_id='{$staff_id}'");
			// 跳转
			if ($personnel_type == 2) {
				Helper_Client::location("变动成功", "index.php?a=external.list&personnel_type={$personnel_type}");
			}else{
				Helper_Client::location("变动成功", "index.php?a=change.list&staff_id={$staff_id}&personnel_type={$personnel_type}");
			}
		}
		$this->tpl->assign("staff_data", $staff_data);

		$this->tpl->display("change/m.change.add.tpl.php");
	}

}

?>